package com.syq.face.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;
import com.syq.face.R;
import com.syq.face.model.AttendanceInfo;
import com.syq.face.model.ManagementGroupInfo;

import java.util.List;

public class AttendanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final String TAG = "AttendanceAdapter";
    private List<AttendanceInfo> attendanceInfoList;
    private AttendanceAdapter.AttendanceViewHolder.OnItemClickListener onItemClickListener;
    private AttendanceAdapter.AttendanceViewHolder.OnItemClickListener onButtonClickListener;


    public List<AttendanceInfo> getAttendanceInfoList() {
        return attendanceInfoList;
    }

    public void setOnButtonClickListener(AttendanceViewHolder.OnItemClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }

    public void setAttendanceInfoList(List<AttendanceInfo> attendanceInfoList) {
        this.attendanceInfoList = attendanceInfoList;
    }

    public void setOnItemClickListener(AttendanceViewHolder.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void notifyItemStatusChanged(int position){
        //代表考勤的状态码
        attendanceInfoList.get(position).setStatus(0);
        notifyItemChanged(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder: ");
        return new AttendanceViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.attendance_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder: ");
        if (holder instanceof AttendanceViewHolder) {
            AttendanceInfo attendanceInfo = attendanceInfoList.get(position);
            Log.d(TAG, "onBindViewHolder: " + attendanceInfo.toString());
            ((AttendanceViewHolder) holder).startTextView.setText("开始时间："+attendanceInfo.getStartTime().toString());
     //       ((AttendanceViewHolder) holder).createTextView.setText("创建时间："+attendanceInfo.getEndTime().toString());
            ((AttendanceViewHolder) holder).endTextView.setText("结束时间："+attendanceInfo.getEndTime().toString());


            if (attendanceInfo.getStatus() == 1) {
                ((AttendanceViewHolder) holder).statusTextView.setText("待考勤");
                ((AttendanceViewHolder) holder).statusTextView.setVisibility(View.VISIBLE);
                ((AttendanceViewHolder) holder).attendanceBtn.setVisibility(View.GONE);

            } else if (attendanceInfo.getStatus() == 0) {
                ((AttendanceViewHolder) holder).statusTextView.setText("完成考勤");
                ((AttendanceViewHolder) holder).statusTextView.setVisibility(View.VISIBLE);
                ((AttendanceViewHolder) holder).attendanceBtn.setVisibility(View.GONE);
            } else {
                ((AttendanceViewHolder) holder).attendanceBtn.setText("正在考勤");
                ((AttendanceViewHolder) holder).statusTextView.setVisibility(View.GONE);
                ((AttendanceViewHolder) holder).attendanceBtn.setVisibility(View.VISIBLE);

                ((AttendanceViewHolder) holder).attendanceBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(onButtonClickListener != null){
                            onButtonClickListener.onItemClick(position,attendanceInfo.getId());
                            Log.d(TAG, "onClick: attendanceInfo"+attendanceInfo.getId());
                        }
                    }
                });

                ((AttendanceAdapter.AttendanceViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick(position, attendanceInfo.getGroupId());
                        }
                    }
                });
            }

        }

    }

    @Override
    public int getItemCount() {
        int size = attendanceInfoList == null ? 0 : attendanceInfoList.size();
        return size;
    }


    public static class AttendanceViewHolder extends RecyclerView.ViewHolder {
        private TextView startTextView;
 //       private TextView createTextView;
        private TextView statusTextView;
        private TextView endTextView;
        private MaterialButton attendanceBtn;


        public AttendanceViewHolder(@NonNull View itemView) {
            super(itemView);

            startTextView = itemView.findViewById(R.id.attendance_item_start_time);
     //       createTextView = itemView.findViewById(R.id.attendance_item_create_time);
            statusTextView = itemView.findViewById(R.id.attendance_item_status);
            endTextView = itemView.findViewById(R.id.attendance_item_end_time);
            attendanceBtn = itemView.findViewById(R.id.attendance_item_btn);


        }

        public interface OnItemClickListener {
            void onItemClick(int position, String userId);
        }

    }
}
